package com.example.module_main.loading

import android.animation.ValueAnimator
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.graphics.Canvas
import android.graphics.Paint
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import android.view.animation.LinearInterpolator
import androidx.core.content.ContextCompat.startActivity
import com.example.lib_common.utils.ToastUtils
import com.example.module_main.R
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.cancel
import kotlinx.coroutines.launch
import java.lang.ref.WeakReference

class MyView(context : Context,attributeSet: AttributeSet) : View(context,attributeSet) {
    //画笔
    private var paint : Paint
    private var paintText : Paint
    private var time : Int = 5
    private var process = 0f
    //避免线程冲突
    private val scope = CoroutineScope(Dispatchers.Main)

    private var activityRef: WeakReference<Activity>? = null




    private var hasJumped = false // 标记是否已跳转

    var animaProcess: ValueAnimator? = null
    var animaTime: ValueAnimator? = null

    init {
        paint = Paint()
        paint.color = context.getColor(R.color.white)
        paint.textSize = 20f
        paint.style = Paint.Style.STROKE
        paint.strokeWidth = 3f

        paintText = Paint()
        paintText.color = context.getColor(R.color.white)
        paintText.textSize = 50f
        paintText.style = Paint.Style.STROKE
        paintText.strokeWidth = 5f
    }
    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        canvas.drawArc(0f,0f,100f,100f,0f,process,false,paint)
        canvas.drawText(time.toString(),38f,62f,paintText)
    }

    fun attachActivity(activity: Activity) {
        activityRef = WeakReference(activity)
    }

    private fun finishActivity() {
        activityRef?.get()?.finish()
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        //五秒倒计时
        startCountDown()
    }


    fun startCountDown(){
        scope.launch {
            animaProcess = ValueAnimator.ofFloat(360f,0f).apply {
                animaProcess?.addUpdateListener {
                    process = it.animatedValue as Float
                    invalidate()
                }
                animaProcess?.interpolator = LinearInterpolator()
                animaProcess?.duration = time*1000L
                animaProcess?.start()
            }


            animaTime = ValueAnimator.ofInt(time,0).apply {
                addUpdateListener {
                    time = animatedValue as Int
                    invalidate()
                    if(time == 0  &&  !hasJumped){
                        hasJumped = true
                        val intent = Intent(context,LoadingActivity::class.java)
                        startActivity(context,intent,null)
                        finishActivity()
                    }
                }
                animaTime?.interpolator = LinearInterpolator()
                animaTime?.duration = time*1000L
                animaTime?.start()
            }
        }
    }


    override fun onTouchEvent(event: MotionEvent?): Boolean {
        when(event?.action){
            MotionEvent.ACTION_DOWN -> {
                ToastUtils.showShort(context,"跳转准备中......")
                return true
            }
            MotionEvent.ACTION_UP -> {
                animaTime?.run {
                    removeAllUpdateListeners()
                    cancel()
                }
                animaProcess?.run {
                    removeAllUpdateListeners()
                    cancel()
                }

                if (!hasJumped) {
                    hasJumped = true
                    context.startActivity(Intent(context, LoadingActivity::class.java))
                    finishActivity()
                }
                ToastUtils.showShort(context,"跳转成功")
                return true
            }
        }
        return super.onTouchEvent(event)
    }

    fun release() {
        // 取消所有动画
        animaTime?.cancel()
        animaProcess?.cancel()
        scope.cancel() // 取消协程
    }
}
